var mainImg;
var albumId;
var imgToDel;
var image_id;

chooseImage = function(url,imgId){
    mainImg.setAttribute('src',url);
    displayInfo(imgId);
    image_id = imgId;
    
}

load = function(){
    //document.getElementById('left-upper-center').style.width = '70%';
    //document.getElementById('right-upper-center').style.width = '25%';
    mainImg = document.getElementById('main-img');
    var images = document.getElementById('inner-wrap');
    var img;
    /*for(var i=0; i<images.children.length; i++){
		img = images.children[i];
		var src = img.getAttribute('src');
		img.onclick = (function(src) { chooseImage(src)})(src);
	}*/
    if(images.children.length>0)  image_id = images.children[0].getAttribute('id').substr(4);
    for(var i=0; i<images.children.length; i++){
        (function(img) {
            var src = img.getAttribute('src');
            var id = img.getAttribute('id').substr(4);
            img.onclick = function() {
                chooseImage(src,id)
            }
        })(images.children[i]);
    }

    imgDisplay = true;

    var effectImages = document.getElementsByClassName('effect-img');
    for(i=0; i<effectImages.length; i++){
        filter(effectImages[i].id,effectImages[i].id);
    }
    
    albumId = document.getElementById('albumId').value;

//    var albumId =  document.getElementById('albumId').value;
//    var xmlurl = "XMLGetController?action=xml&fileName=album-" + albumId;
//    xml = loadXML(xmlurl);
}

imgToggle = function(){
    mainImg.style.visibility = imgDisplay?'hidden':'visible';
    imgDisplay = !imgDisplay;
}

/*window.onresize = function(event) {
	var bo = document.getElementsByTagName('body');
    if(window.innerHeight < 625){
		bo.style.overflow = 'auto';
	} else {
		bo.style.overflow = 'hidden';
	}
}*/

filter = function (elementId, filterType){
    var el = document.getElementById(elementId);
    switch (filterType){
        case 'normal':
            el.style.filter = '';
            break;
        case 'flip-horizontal':
            el.style.filter = 'fliph';
            break;
        case 'flip-vertical':
            el.style.filter = 'flipv';
            break;
        case 'wave':
            el.style.filter = 'wave(add = 0, freq = 5, lightstrength = 10, phase = 100, strength = 20)';
            break;
        case 'fade':
            el.style.filter = 'alpha(opacity = 100, finishopacity = 0, style = 3, startx = 20, starty = 40, finishx = 0, finishy = 0)';
            break;
        case 'xray':
            el.style.filter = 'xray';
            break;
        case 'blur':
            el.style.filter = 'blur(add = 0, direction = 300, strength = 10)';
            break;
        case 'gray':
            el.style.filter = 'gray';
            break;
        case 'invert':
            el.style.filter = 'invert';
            break;
        case 'rotate-90':
            el.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation=1)';
            break;
        case 'rotate-180':
            el.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation=2)';
            break;
        case 'rotate-270':
            el.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation=3)';
            break;
        default:
            break;
    }

    if (elementId == 'main-img'){
        if (filterType == 'rotate-90' || filterType == 'rotate-270'){
            el.style.maxWidth = '450px';
        } else {
            el.style.maxWidth = '90%';
        }
    }
}

function loadXML(dname)
{
    if (window.XMLHttpRequest)
      {
          xhttp=new XMLHttpRequest();
      }
    else
      {
          xhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xhttp.open("GET",dname,false);
    xhttp.send(null);
    return xhttp.responseXML;
}

displayInfo = function (imgId){
    

    if (window.ActiveXObject)
    {        
//        var xmldoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
//        xmldoc.async = false;
//        xmldoc.load("XMLGetController?action=xml&fileName=album-"+albumId);
        var xmldoc = loadXML("XMLGetController?action=xml&fileName=album-"+albumId);
        var xpath = '//image[@id='+imgId+']';
        var node = xmldoc.selectSingleNode(xpath);
        var children = node.childNodes;
        for(i=0; i<children.length; i++){
            var child = children[i];
            if(child.tagName == 'caption'){
                document.getElementById('caption').innerHTML = child.text;
            }
            if(child.tagName == 'takenDate'){
                document.getElementById('imageTakenDate').innerHTML = '<span id="imageCaption">' + child.text + '</span>';
            }
        }
    }
}

deleteAlbum = function(){
    if(confirm('Delete this album?')){
        var url = "DefaultController?action=delete_album&albumId="+albumId;
        window.location.href = url;
    }
}

deleteImage = function(){
    if(confirm('Delete this image?')){        
        var url = "DefaultController?action=delete_image&imageId="+image_id+"&albumId="+albumId;
        window.location.href = url;
    }
}